#include <stdio.h>
#include <memory.h>

#define MAX_BLOCK   5
#define MAX_SIZE    2187    /* 3^7 */

static char block[MAX_BLOCK][MAX_BLOCK];
static int  block_size;
static char output[MAX_SIZE][MAX_SIZE+1];

static int intpow(int x, int n)
{
    int i, ret = 1;
    for (i = 0; i < n; i++)
        ret *= x;
    return ret;
}

static void build(int scale, int blank, int r, int c)
{
    int i, j;
    if (scale == 1) /* final level */
    {
        for (i = 0; i < block_size; i++)
        {
            if (blank)
                memset(&output[r+i][c], ' ', block_size);
            else
                memcpy(&output[r+i][c], block[i], block_size);
        }
    }
    else
    {
        int size = intpow(block_size, scale - 1);
        int rr = r;
        for (i = 0; i < block_size; i++, rr += size)
        {
            int cc = c;
            for (j = 0; j < block_size; j++, cc += size)
                build(scale - 1, blank || block[i][j] == ' ', rr, cc);
        }
    }
}

static void print(int scale)
{
    int i, size = intpow(block_size, scale);
    for (i = 0; i < size; i++)
    {
        output[i][size] = '\n';
        fwrite(output[i], 1, size+1, stdout);
    }
}

int main()
{
    char line[100];
    while (fgets(line, sizeof(line), stdin))
    {
        int i, scale;

        sscanf(line, "%d", &block_size);
        if (block_size == 0)
            break;

        for (i = 0; i < block_size; i++)
        {
            fgets(line, sizeof(line), stdin);
            memcpy(block[i], line, block_size);
        }
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &scale);

        build(scale, 0, 0, 0);
        print(scale);
    }
    return 0;
}
